// File:    TnExToolsSpatialRel.cs
// Author:  HT
// Created: Tuesday, October 18, 2011 3:51:07 PM
// Purpose: Definition of Class TnExToolsSpatialRel

using System;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
namespace Tayninh2011
{
    public class TnExToolsSpatialRel : TnExtensionTools, ITnExToolsSpatialRel
    {
        int ITnExToolsSpatialRel.TnIntersection(ref TnThua inputFClass, IFeature intersectFeature)
        {
            int intSelectedFeature = 0;
            //IFeatureCursor featureCursor = intersect_feature.Search(filter_on_intersect_feature, true);
            //lay con tro chi doi tuong cho lop select
            //IFeature selectFeature = featureCursor.NextFeature();

            SpatialFilter spatialFilter = new SpatialFilter();

            IFeatureLayer inputLayer = new FeatureLayerClass();
            inputLayer.FeatureClass = inputFClass.FeatureClass;
            inputFClass.FeatureSelection = (IFeatureSelection)inputLayer;

            IFeatureLayerDefinition flDef = (IFeatureLayerDefinition)inputLayer;

            object obj = Type.Missing;

            //IGeometryCollection geometryCollection = new GeometryBagClass();
            //geometryCollection.AddGeometry(intersect_feature.Shape, ref obj, ref obj);

            spatialFilter.Geometry = intersectFeature.Shape;
            spatialFilter.GeometryField = inputFClass.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

            inputFClass.FeatureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            inputFClass.FeatureLayer = flDef.CreateSelectionLayer("newlayer", true, null, null);
            //selectClass = selectLayer.FeatureClass;

            intSelectedFeature = inputFClass.FeatureSelection.SelectionSet.Count;


            return intSelectedFeature;
        }

        int ITnExToolsSpatialRel.TnIntersection(ref TnDuong inputFClass, IFeature intersectFeature)
        {
            int intSelectedFeature = 0;
            //IFeatureCursor featureCursor = intersect_feature.Search(filter_on_intersect_feature, true);
            //lay con tro chi doi tuong cho lop select
            //IFeature selectFeature = featureCursor.NextFeature();

            SpatialFilter spatialFilter = new SpatialFilter();

            IFeatureLayer inputLayer = new FeatureLayerClass();
            inputLayer.FeatureClass = inputFClass.FeatureClass;
            inputFClass.FeatureSelection = (IFeatureSelection)inputLayer;

            IFeatureLayerDefinition flDef = (IFeatureLayerDefinition)inputLayer;

            object obj = Type.Missing;

            //IGeometryCollection geometryCollection = new GeometryBagClass();
            //geometryCollection.AddGeometry(intersect_feature.Shape, ref obj, ref obj);

            spatialFilter.Geometry = intersectFeature.Shape;
            spatialFilter.GeometryField = inputFClass.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

            inputFClass.FeatureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            inputFClass.FeatureLayer = flDef.CreateSelectionLayer("newlayer", true, null, null);
            //selectClass = selectLayer.FeatureClass;

            intSelectedFeature = inputFClass.FeatureSelection.SelectionSet.Count;


            return intSelectedFeature;
        }

        int ITnExToolsSpatialRel.TnIntersection(ref TnHem inputFClass, IFeature intersectFeature)
        {
            int intSelectedFeature = 0;
            //IFeatureCursor featureCursor = intersect_feature.Search(filter_on_intersect_feature, true);
            //lay con tro chi doi tuong cho lop select
            //IFeature selectFeature = featureCursor.NextFeature();

            SpatialFilter spatialFilter = new SpatialFilter();

            IFeatureLayer inputLayer = new FeatureLayerClass();
            inputLayer.FeatureClass = inputFClass.FeatureClass;
            inputFClass.FeatureSelection = (IFeatureSelection)inputLayer;

            IFeatureLayerDefinition flDef = (IFeatureLayerDefinition)inputLayer;

            object obj = Type.Missing;

            //IGeometryCollection geometryCollection = new GeometryBagClass();
            //geometryCollection.AddGeometry(intersect_feature.Shape, ref obj, ref obj);

            spatialFilter.Geometry = intersectFeature.Shape;
            spatialFilter.GeometryField = inputFClass.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

            inputFClass.FeatureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            inputFClass.FeatureLayer = flDef.CreateSelectionLayer("newlayer", true, null, null);
            //selectClass = selectLayer.FeatureClass;

            intSelectedFeature = inputFClass.FeatureSelection.SelectionSet.Count;


            return intSelectedFeature;
        }

        int ITnExToolsSpatialRel.TnWithin(ref TnThua inputFClass, IFeature containFeature)
        {
            int intSelectedFeature = 0;
            //IFeatureCursor featureCursor = intersect_feature.Search(filter_on_intersect_feature, true);
            //lay con tro chi doi tuong cho lop select
            //IFeature selectFeature = featureCursor.NextFeature();

            SpatialFilter spatialFilter = new SpatialFilter();

            IFeatureLayer inputLayer = new FeatureLayerClass();
            inputLayer.FeatureClass = inputFClass.FeatureClass;
            inputFClass.FeatureSelection = (IFeatureSelection)inputLayer;

            IFeatureLayerDefinition flDef = (IFeatureLayerDefinition)inputLayer;

            object obj = Type.Missing;

            //IGeometryCollection geometryCollection = new GeometryBagClass();
            //geometryCollection.AddGeometry(intersect_feature.Shape, ref obj, ref obj);

            spatialFilter.Geometry = containFeature.Shape;
            spatialFilter.GeometryField = inputFClass.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            inputFClass.FeatureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            inputFClass.FeatureLayer = flDef.CreateSelectionLayer("newlayer", true, null, null);
            //selectClass = selectLayer.FeatureClass;

            intSelectedFeature = inputFClass.FeatureSelection.SelectionSet.Count;


            return intSelectedFeature;
        }

        int ITnExToolsSpatialRel.TnWithin(ref TnDuong inputFClass, IFeature containFeature)
        {
            int intSelectedFeature = 0;
            //IFeatureCursor featureCursor = intersect_feature.Search(filter_on_intersect_feature, true);
            //lay con tro chi doi tuong cho lop select
            //IFeature selectFeature = featureCursor.NextFeature();

            SpatialFilter spatialFilter = new SpatialFilter();

            IFeatureLayer inputLayer = new FeatureLayerClass();
            inputLayer.FeatureClass = inputFClass.FeatureClass;
            inputFClass.FeatureSelection = (IFeatureSelection)inputLayer;

            IFeatureLayerDefinition flDef = (IFeatureLayerDefinition)inputLayer;

            object obj = Type.Missing;

            //IGeometryCollection geometryCollection = new GeometryBagClass();
            //geometryCollection.AddGeometry(intersect_feature.Shape, ref obj, ref obj);

            spatialFilter.Geometry = containFeature.Shape;
            spatialFilter.GeometryField = inputFClass.FeatureClass.ShapeFieldName;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            inputFClass.FeatureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            inputFClass.FeatureLayer = flDef.CreateSelectionLayer("newlayer", true, null, null);
            //selectClass = selectLayer.FeatureClass;

            intSelectedFeature = inputFClass.FeatureSelection.SelectionSet.Count;


            return intSelectedFeature;
        }

    }
}
